package com.projectName.www.service;

import com.projectName.www.po.dishes;
import com.projectName.www.util.find;
import com.projectName.www.dao.S_J_insertdishes_dao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import java.awt.*;

    /**
     * @author 86132
     *
     */
    public class S_J_insertdishes extends JInternalFrame {
        private JTextField name;
        private JTextField des;
        private JTextField money;
       public dishes d;
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        S_J_insertdishes frame = new S_J_insertdishes();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public S_J_insertdishes() {

            setTitle("增加菜名");
            setMaximizable(true);
            setClosable(true);

            setBounds(100, 100, 696, 437);

            JDesktopPane desktopPane = new JDesktopPane();
            desktopPane.setBackground(SystemColor.menu);
            GroupLayout groupLayout = new GroupLayout(getContentPane());
            groupLayout.setHorizontalGroup(
                    groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                    .addComponent(desktopPane, GroupLayout.PREFERRED_SIZE, 687, GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            groupLayout.setVerticalGroup(
                    groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                    .addComponent(desktopPane, GroupLayout.PREFERRED_SIZE, 409, GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            name = new JTextField();
            name.setBounds(218, 51, 315, 32);
            desktopPane.add(name);
            name.setColumns(10);

            JLabel lblNewLabel_1 = new JLabel("菜名：");
            lblNewLabel_1.setBounds(139, 50, 101, 32);
            desktopPane.add(lblNewLabel_1);

            JLabel lblNewLabel_1_1 = new JLabel("描述：");
            lblNewLabel_1_1.setBounds(139, 144, 101, 32);
            desktopPane.add(lblNewLabel_1_1);

            des = new JTextField();
            des.setColumns(10);
            des.setBounds(218, 118, 315, 85);
            desktopPane.add(des);

            JLabel lblNewLabel_1_1_1 = new JLabel("价格：");
            lblNewLabel_1_1_1.setBounds(139, 235, 101, 32);
            desktopPane.add(lblNewLabel_1_1_1);

            money = new JTextField();
            money.setColumns(10);
            money.setBounds(218, 236, 135, 32);
            desktopPane.add(money);

            JButton insert = new JButton("添加");
            insert.addActionListener(e -> {
                //添加

                try {
                    d=new dishes(name.getText(),des.getText(), find.name(OpenBusiness.username),money.getText());
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }


                try {
                    if (S_J_insertdishes_dao.insertdishes(d)) {
                        JOptionPane.showMessageDialog(null, "添加成功");
                        return;
                    } else {
                        JOptionPane.showMessageDialog(null, "添加失败:名字为空或者重复");
                    }
                    return;
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }

            });

            insert.setBounds(139, 297, 127, 56);
            desktopPane.add(insert);

            JButton close = new JButton("取消");
            //取消
            close.addActionListener(e -> {
                name.setText("");
                des.setText("");
                money.setText("");
            });
            close.setBounds(375, 297, 127, 56);
            desktopPane.add(close);
            getContentPane().setLayout(groupLayout);

        }

    }

